/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import entities.Basics;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author HTH2207
 */
@Stateless
public class BasicsStateless implements BasicsStatelessLocal {

    @PersistenceContext(unitName = "TravelLover-ejbPU")
    private EntityManager entityManager;

    public BasicsStateless() {
    }

    public void addSentence(String ID, String sEnglish, String sVietnam, String audio, String image) {
        Basics b = new Basics(ID, sEnglish, sVietnam, audio, image);
        entityManager.merge(b);
    }

    public void removeSentence(String ID) {
        Basics b = entityManager.find(Basics.class, ID);
        entityManager.remove(b);
    }

    public List<Basics> getAllSentence() {
        Query query = entityManager.createQuery("Select b From Basics b");
        return query.getResultList();
    }

    public List<Basics> findByES(String sEnglish) {
        Query query = entityManager.createQuery("Select b From Basics b Where b.English Like ?");
        query.setParameter(1, sEnglish);
        return query.getResultList();
    }

    public List<Basics> finByVS(String sVietnam) {
        Query query = entityManager.createQuery("Select b From Basics b Where b.VietNamese Like ?");
        query.setParameter(2, sVietnam);
        return query.getResultList();
    }

    public void updateSentence(String sID, String sEnglish, String sVietNam, String sAudio, String sImage) {
        Basics b = entityManager.find(Basics.class, sID);
        b.setEnglish(sEnglish);
        b.setVietNamese(sVietNam);
        b.setAudio(sAudio);
        b.setImage(sImage);
    }
}
